package com.novanok.baniosound;

import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.LinearLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class MyActivity extends Activity {

    private AdView adView;
    private InterstitialAd interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-8819200492193671/9035203540");
        adView.setAdSize(AdSize.BANNER);
        LinearLayout layout = (LinearLayout)findViewById(R.id.banner);
        layout.addView(adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-8819200492193671/7279268744");

        AdRequest adRequest2 = new AdRequest.Builder().build();
        interstitial.loadAd(adRequest2);
    }

    public void reproducir( View v ){
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        int cantidad = sharedPref.getInt("cantidad_click", 0);

        if ( cantidad > 3 ){
            if ( interstitial.isLoaded() ) {
                interstitial.show();
            }

            cantidad = 0;
        }

        MediaPlayer mPlayer = MediaPlayer.create(MyActivity.this, R.raw.inodoro);
        mPlayer.start();

        cantidad++;

        SharedPreferences.Editor ed = sharedPref.edit();
        ed.putInt("cantidad_click", cantidad);
        ed.apply();
    }
}
